
import axios from 'axios'
import { Configuration, Logger } from 'log4js'
import { MongoClient } from 'mongodb'
import path from 'path'

//配置文件加载类
export interface ConfigInterface {
    dbUrl: string
    dbName: string
    logger: Configuration
    access: string
    secret: string
    bucket: string
    map: Map<string, string>
}

//加载配置文件
export async function loadConfig<T>(filepath: string) {
    const fullpath = path.isAbsolute(filepath) ? filepath : path.join(process.cwd(), filepath)
    delete require.cache[require.resolve(fullpath)]
    const ret = await import(fullpath) as T
    return ret
}
type colType = 'basic' | 'detail' | 'statisticQueue' | 'transformQueue' | 'imgQueue' | 'opgg' | 'runes' | 'item' | 'skill' | 'build' | 'imgs' | 'img' | 'basicQueue'
export class connecDB {
    constructor(
        private dbUrl: string,
        private dbName: string
    ) { }

    async mongoCol<T>(colName: colType) {
        return (await MongoClient.connect(this.dbUrl)).db(this.dbName).collection<T>(colName)
    }
}

export function sleep(secs: number) {
    return new Promise(resolve => setTimeout(resolve, secs * 100))
}

export async function getAxios(url: string): Promise<string> {
    try {
        const res = await axios.get(url)
        if (res.status !== 200) {
            return "404"
        }
        return res.data
    } catch (error) {

        return "404"
    }
}
